/**
 * http://code.google.com/p/gwt-spot
 * 
 * GWT-Spot -- Copyright 2008-2009
 *          -- by Dunkel, Ricardo
 *  
 */
package de.ricardodunkel.gwtspot.client.api.ui.common;

import com.google.gwt.user.client.ui.ButtonBase;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.HorizontalSplitPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TextBoxBase;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.VerticalSplitPanel;
import com.google.gwt.user.client.ui.Widget;

import de.ricardodunkel.gwtspot.client.api.common.StringUtility;

/**
 * Common widget utility class for more comfortable usage.
 * 
 * @author Dunkel, Ricardo
 * 
 * @version 0.1
 * 
 */
public class WidgetUtility {
	
	// -------------------------- BASiC WIDGETS -------------------------------
	/**
	 * Does create a styled {@link HTML} element.
	 * 
	 * @param text  A text to set for element description.
	 * @param style  A style which may used for element.
	 * 
	 * @return  An element with given text and style.
	 */
	public static HTML createHTML(String text, String style) {
		HTML res = new HTML( text );
		res.setStyleName( style );
		return res;
	}
	
	/**
	 * Does create a styled {@link HTML} element with default 
	 * text and title.
	 * 
	 * @param title  A title used for creation of element.
	 * @param text  A text to set for element description.
	 * @param style  A style which may used for element.
	 * 
	 * @return  An element with given text, title and style.
	 */
	public static HTML createHTML(String title, String text, String style) {
		HTML res = new HTML( text );
		res.setTitle( title );
		res.setStyleName( style );
		return res;
	}
	
	/**
	 * Create a {@link Label} element by desired values.
	 *  
	 * @param title  A title used for creation of element.
	 * @param text  A text to set for element description.
	 * @param style  A style which may used for element.
	 * 
	 * @return  An element with given text, title and style.
	 */
	public static Label createLabel(String title, String text, String style) {
		Label label = new Label(title);
		label.setText(text);
		label.setStyleName(style);
		return label;
	}
	

	// -------------------------- COMMON HELPER -------------------------------
	/**
	 * Add widget text property for desired element.
	 * 
	 * @param tbb  An object of {@link TextBoxBase} to manipulate.
	 * @param text  A text to set for element description, may be null.
	 */
	public static void setWidgetText(TextBoxBase tbb, String text) {
		if( null != tbb && StringUtility.isNotEmpty(text) ) {
			tbb.setText( text );
		}
	}
	
	/**
	 * Add widget text property for desired element.
	 * 
	 * @param tbb  An object of {@link ButtonBase} to manipulate.
	 * @param text  A text to set for element description, may be null.
	 */
	public static void setWidgetText(ButtonBase bb, String text) {
		if( null != bb && StringUtility.isNotEmpty(text) ) {
			bb.setText( text );
		}
	}
	
	/**
	 * Add widget text property for dialog box.
	 * 
	 * @param db  An object of {@link DialogBox} to manipulate.
	 * @param text  A text to set for element, may be null.
	 */
	public static void setWidgetText(DialogBox db, String text) {
		if( null != db && StringUtility.isNotEmpty(text) ) {
			db.setText( text );
		}
	}
	
	/**
	 * Add widget text property for suggest box.
	 * 
	 * @param db  An object of {@link SuggestBox} to manipulate.
	 * @param text  A text to set for element, may be null.
	 */
	public static void setWidgetText(SuggestBox sb, String text) {
		if( null != sb && StringUtility.isNotEmpty(text) ) {
			sb.setText( text );
		}
	}
	
	/**
	 * Add widget properties, like title or style for a given element.
	 * 
	 * @param w  A {@link Widget} to set properties for.
	 * @param title  A title used for creation of {@link TextBox}, 
	 * 				 may be null but should not or must be set later on.
	 * @param style  A style which may used for element, may be null.
	 */
	public static void setWidgetProperties(Widget w, String title, String style) {
		if( null != w && StringUtility.isNotEmpty(title) ) {
			w.setTitle( title );
		}
		if( null != w && StringUtility.isNotEmpty(style) ) {
			w.setStyleName( style );
		}
	}
	
	
	// -------------------------- PANEL SECTION -------------------------------
	/**
	 * Does create a default panel.
	 * 
	 * @return  A panel of type {@link HorizontalPanel}.
	 */
	public static Panel createPanel() {
		return createPanel(false, false);
	}

	/**
	 * Does create a default panel of {@link HorizontalPanel}
	 * within a given style.
	 * 
	 * @param style  A style to use for panel as default.
	 * 
	 * @return  A styled panel of type {@link HorizontalPanel}.
	 */
	public static Panel createPanel(String style) {
		return createPanel(false, false, style);
	}
	
	/**
	 * Does create a default panel.
	 * 
	 * @param vertical  If <strong>true</strong> {@link VerticalPanel}, otherwise {@link HorizontalPanel}.
	 * 
	 * @return  A panel of desired type.
	 */
	public static Panel createPanel(boolean vertical) {
		return createPanel(vertical, false);
	}
	
	/**
	 * Does create a default panel which may be vertical,  horizontal, splitted or not.
	 * 
	 * @param vertical  If <strong>true</strong> a vertical one, otherwise a horizontal one. 
	 * @param split  If <strong>true</strong> a split panel, otherwise standard.
	 * 
	 * @return  A panel of desired type.
	 */
	public static Panel createPanel(boolean vertical, boolean split) {
		return createPanel(vertical, split, null);
	}
	
	/**
	 * Does create a desired panel, controlled by given parameters.
	 * 
	 * @param vertical  If <strong>true</strong> a vertical one, otherwise a horizontal one. 
	 * @param split  If <strong>true</strong> a split panel, otherwise standard.
	 * @param style  A style to use for panel as default.
	 * 
	 * @return  A panel of desired type.
	 */
	public static Panel createPanel(boolean vertical, boolean split, String style) {
		Panel p;
		if( vertical ) {
			if( split ) {
				p = new VerticalSplitPanel();		
			} else {
				p = new VerticalPanel();
			}
		} else {
			if( split ) {
				p = new HorizontalSplitPanel();
			} else {
				p = new HorizontalPanel();
			}
		}
		if( StringUtility.isNotBlank(style) ) {
			p.setStyleName( style );
		}
		return p;
	}
	
	// -------------------------- EXTENDED PANEL SECTION ----------------------
	/**
	 * Does create a <em>texted</em> widget by defaults.
	 * 
	 * @param w  A widget to use manipulation.
	 * 
	 * @return  An enriched panel.
	 */
	public static ComplexPanel createTextedWidget(Widget w) {
		return createTextedWidget(w,null);
	}
	
	/**
	 * Does create a <em>texted</em> widget by defaults and 
	 * adding an identity to element.
	 * 
	 * @param w  A widget to use manipulation.
	 * @param textid  An ID to set optionally.
	 * 
	 * @return  An enriched panel.
	 */
	public static ComplexPanel createTextedWidget(Widget w, String textid) {
		return createTextedWidget(w, textid, null);
	}

	/**
	 * Does generate an <em>extended texted</em> widget.
	 * 
	 * @param w  A widget to use manipulation.
	 * @param textid  An ID to set optionally.
	 * @param width  A width to use for widget creation.
	 * 
	 * @return  An enriched panel.
	 */
	public static ComplexPanel createTextedWidget(Widget w, String textid, String width) {
		return createTextedWidget(w, textid, width, null);
	}
	
	/**
	 * Does generate a <em>full qualified texted</em> widget.
	 *  
	 * @param w  A widget to use manipulation.
	 * @param textid  An ID to set optionally.
	 * @param width  A width to use for widget creation.
	 * @param textstyle  A style which may be used for text modification.
	 * 
	 * @@return  An enriched panel.
	 */
	public static ComplexPanel createTextedWidget(Widget w, String textid, String width, String textstyle) {
		HorizontalPanel h = new HorizontalPanel();
		if( StringUtility.isNotEmpty(width) ) {
			w.setWidth( width );
		}
		h.add( createTextedLabel(w.getTitle(),textid,width,textstyle) );
		h.add( w );
		return h;
	}
	
	/**
	 * Retrieve an already prepared <em>basic</em> label.
	 * 
	 * @param title  A title for <em>texted</em> label.
	 * @param textid  An ID to set optionally.
	 * 
	 * @return  A prepared {@link HTML} widget.
	 */
	public static HTML createTextedLabel(String title, String textid) {
		return createTextedLabel(title, textid, null);
	}
	
	/**
	 * Retrieve an already prepared <em>standard</em> label within
	 * an identity and width.
	 * 
	 * @param title  A title for <em>texted</em> label.
	 * @param textid  An ID to set optionally.
	 * @param width  A width to use for label creation.
	 * 
	 * @return  A prepared {@link HTML} widget.
	 */
	public static HTML createTextedLabel(String title, String textid, String width) {
		return createTextedLabel(title, textid, width, null);
	}
	
	/**
	 * Retrieve an already prepared <em>full qualified</em> label.
	 * 
	 * @param title  A title for <em>texted</em> label.
	 * @param textid  An ID to set optionally.
	 * @param width  A width value to set for element.
	 * @param style  A style to set for <em>texted</em> label.
	 * 
	 * @return  A prepared {@link HTML} widget.
	 */
	public static HTML createTextedLabel(String title, String textid, String width, String style) {
		HTML html = new HTML(title + ":");
		if( StringUtility.isNotEmpty(textid) ) {
			html.getElement().setId(textid);
		}
		if( StringUtility.isNotEmpty(width) ) {
			html.setWidth( width );
		}
		if( StringUtility.isNotEmpty(style) ) {
			html.setStyleName( style );
		}
		return html;
	}
	
}
